package main

import (
	"fmt"

	"gitlab.w2gou.cn/xbc/go-lib/log"
	"gitlab.w2gou.cn/xbc/go-lib/util"
)

func testRbtree1() {
	tree := NewTree()
	tree.Insert("k1", []byte("1"))
	tree.Insert("k2", []byte("12"))
	tree.Insert("k3", []byte("123"))
	tree.Insert("k4", []byte("123"))
	tree.Insert("k5", []byte("123"))
	tree.Insert("k6", []byte("123"))

	tree.DumpPng("1.txt")

	tree.Delete("k2")

	tree.DumpPng("2.txt")
	log.Info4("", "", "str", tree.DumpKey())
}

func testRbtree2() {
	tree := NewTree()
	lines := util.ReadFileLines("rand1.txt")

	for i, line := range lines {
		//if i == 20 { break }

		tree.Insert(line, "")
		_ = i
	}
	tree.DumpPng("all.txt")
	log.Info4("", "dumpall", "str", tree.DumpKey())
	return

	dels := util.ReadFileLines("del_index.txt")
	for _, del := range dels {
		i := util.Atoi(del)
		tree.Delete(lines[i])
	}
	tree.DumpPng("del.txt")

	log.Info4("", "dump del", "str", tree.DumpKey())
}

/*
end.txt 内容如下 digraph {k3->k1;k1->k0;k1->k2;k3->k5;k5->k4;k5->k7;k7->k6;k7->k8;k8->k9;}
从文件中加载命令
dot -Tpng -O end.txt
*/

func (t *Tree) DumpPng(filename string) {
	cmd := fmt.Sprintf("digraph {%v}", t.root.dumpDot())

	//cmdfile := fmt.Sprintf("g%v.txt", i)

	util.WriteFile(filename, cmd)
	util.Cmd(".", "dot", "-Tpng", "-O", filename)

}
